package com.itmk.business.article.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.business.article.entity.Article;
import com.itmk.business.article.entity.ArticleTags;
import com.itmk.business.article.mapper.ArticleMapper;
import com.itmk.business.article.mapper.ArticleTagsMapper;
import com.itmk.business.article.service.ArticleService;
import com.itmk.vo.req.ArticleReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService {

    @Autowired
    private ArticleTagsMapper tagsMapper;

    @Override
    public List<Article> getArticlesByCategoryId(Long categoryId) {
        return this.baseMapper.getArticleListByCategoryId(categoryId);
    }

    @Override
    public Long saveArticle(Article article) {
        return this.baseMapper.saveArticle(article);
    }

    @Override
    public Long updateArticle(Article article) {
        return this.baseMapper.updateArticle(article);
    }

    @Override
    public Article getArticleById(Long id) {
        return this.baseMapper.getArticleById(id);
    }

    @Override
    public IPage<Article> getArticleList(IPage<Article> iPage,ArticleReqVO vo) {
        return this.baseMapper.getArticleList(iPage,vo);
    }

    @Override
    public Boolean displayArticle(Article article) {
        return this.baseMapper.displayArticle(article)>0;
    }

    @Override
    public Boolean SaveArticleTag(Article article) {
        String[] tags=article.getTags().split(",");
        Boolean b=true;
        for (String tag:tags) {
            Integer count=tagsMapper.SelectCountNum(tag,(int)article.getCategoryId());
            if(count==0){
               b= tagsMapper.insert(new ArticleTags(tag,(int)article.getCategoryId()))==1;
            }
        }
        return b;
    }
}
